import request from '@/utils/request';
import {download, toFormData, checkDownloadRes} from '@/utils/common';

/**
 * 分页查询链接列表
 */
export async function pageItems(params) {
  const res = await request.get('/admin/cms/nav/item/list', {params});
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}


/**
 * 查询链接下拉列表
 * @returns {Promise<any>}
 */
export async function dropDownListItems(params) {
  const res = await request.get('/admin/cms/nav/item/dropDownList', {params});
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加链接项目
 */
export async function addItem(data) {
  const res = await request.post('/admin/cms/nav/item', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改菜单项目
 */
export async function updateItem(data) {
  const res = await request.put('/admin/cms/nav/item', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除菜单项目
 */
export async function removeItem(id) {
  const res = await request.delete('/admin/cms/nav/item/' + id);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除菜单项目
 */
export async function removeItems(ids) {
  const res = await request.delete('/admin/cms/nav/item/' + ids.join());
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改链接状态
 */
export async function updateItemsStatus(id, status) {
  const res = await request.put('/admin/cms/nav/item/changeStatus', {
    id,
    status
  });
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}


/**
 * 导出菜单项目
 */
export async function exportItems(params) {
  const res = await request({
    url: '/admin/cms/nav/item/export',
    method: 'POST',
    data: toFormData(params),
    responseType: 'blob'
  });
  await checkDownloadRes(res);
  download(res.data, `item_${Date.now()}.xlsx`);
}
